//buyercar.js
const util = require('../../utils/util.js')
let alls = true;
let shopGoods = true;
Page({
  data: {
    buyerShow: true,
    buyerRest:false,
    checked0: false,
    checked1: false,
    checked2: false,
    checked3: false,
    shorTage: false,
    totalPrice: 0,
    settlement: true,
    shopPrice1: 268,
    shopPrice2: 115,
    quantity1: 2,
    quantity2: 1,
    stop1: true,
    stop2:true
  },
  onLoad: function(){
    let that = this;
    if (that.data.quantity1 > 1) {
      that.setData({
         stop1: false
      })
    } else if (that.data.quantity2 > 1) {
      that.setData({
         stop2: false
      })
    } else {
      that.setData({
        stop1: true,
        stop2:true
      })
    }
  },
  tabShop: function () {
    wx.showModal({
      title: '配送费',
      content: '订单金额满XXX元起送，配送费XX元订单金额满200元，免配送费',
      showCancel:false,
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  shopElection: function () {
     let that = this;
    if (shopGoods) {
      let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
      that.setData({
        checked0: true,
        checked1: true,
        checked2: true,
        checked3: true,
        settlement: false,
        totalPrice:allsPrice
      }, () => { 
          alls = false;
          shopGoods = false;
      })
    } else {
      that.setData({
        checked0: false,
        checked1: false,
        checked2: false,
        checked3: false,
        settlement: true,
        totalPrice:0
      }, () => { 
          alls = true;
          shopGoods = true;
      })
    }
  },
  checkboxChange: function (e) {
    let that = this;
    let array = e.detail.value;
    if (array.length > 0) {
      for (let id of array) {
        if (array.length == 1) {
          if (id == 1) {
            let allsPrice = (that.data.shopPrice1 * that.data.quantity1);
            that.setData({
              checked0: false,
              checked1: true,
              checked2: false,
              checked3: false,
              settlement: false,
              totalPrice: allsPrice
              }, () => {
                alls = true;
                shopGoods = true;
            })
          } else {
            let allsPrice = (that.data.shopPrice2 * that.data.quantity2);
            that.setData({
              checked0: false,
              checked1: false,
              checked2: true,
              checked3: false,
              settlement: false,
              totalPrice: allsPrice
              }, () => {
                alls = true;
                shopGoods = true;
            })
          }
        } else {
          let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
          that.setData({
            checked0: true,
            checked1: true,
            checked2: true,
            checked3: true,
            settlement: false,
            totalPrice: allsPrice
          }, () => {
              alls = false;
              shopGoods = false;
          })
        }
      }
    } else {
      that.setData({
        checked0: false,
        checked1: false,
        checked2: false,
        checked3: false,
        settlement: true,
        totalPrice: 0
        }, () => {
          alls = true;
          shopGoods = true;
      })
    }
  },
  reduce: function (e) {
    let that = this;
    let ids = e.currentTarget.dataset.code;
    let num1 = that.data.quantity1;
    let num2 = that.data.quantity2;
    if (ids == 1) {
      num1--;
      if (num1 < 1) {
          wx.showToast({
            title: '数量不能小于1',
            icon: 'none',
            duration: 2000
          })
      } else if (num1 == 1) {
          that.setData({
            stop1: true,
            quantity1: num1
          })
      } else {
         that.setData({
            stop1: false,
            quantity1: num1
         })
      }
      if (that.data.checked1) {
        if (that.data.checked2) {
            let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            let allsPrice = that.data.shopPrice1 * that.data.quantity1;
            that.setData({
                totalPrice:allsPrice
            })
        }
      } else {
        if (that.data.checked2) {
            let allsPrice = that.data.shopPrice2 * that.data.quantity2;
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            that.setData({
                totalPrice:0
            })
        }
      }
    } else {
      num2--;
      if (num2 < 1) {
          wx.showToast({
            title: '数量不能小于1',
            icon: 'none',
            duration: 2000
          })
      } else if (num2 == 1) {
          that.setData({
            stop2: true,
            quantity2: num2
          })
      } else {
         that.setData({
            stop2: false,
            quantity2: num2
         })
      }
      if (that.data.checked1) {
        if (that.data.checked2) {
            let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            let allsPrice = that.data.shopPrice1 * that.data.quantity1;
            that.setData({
                totalPrice:allsPrice
            })
        }
      } else {
        if (that.data.checked2) {
            let allsPrice = that.data.shopPrice2 * that.data.quantity2;
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            that.setData({
                totalPrice:0
            })
        }
      }
    }
  },
  add: function (e) {
    let that = this;
    let ids = e.currentTarget.dataset.code;
    let num1 = that.data.quantity1;
    let num2 = that.data.quantity2;
    if (ids == 1) {
      num1++;
      that.setData({
        quantity1: num1,
        stop1:false
      })
      if (that.data.checked1) {
        if (that.data.checked2) {
            let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            let allsPrice = that.data.shopPrice1 * that.data.quantity1;
            that.setData({
                totalPrice:allsPrice
            })
        }
      } else {
        if (that.data.checked2) {
            let allsPrice = that.data.shopPrice2 * that.data.quantity2;
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            that.setData({
                totalPrice:0
            })
        }
      }
    } else {
      num2++;
      that.setData({
        quantity2: num2,
        stop2:false
      })
      if (that.data.checked1) {
        if (that.data.checked2) {
            let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            let allsPrice = that.data.shopPrice1 * that.data.quantity1;
            that.setData({
                totalPrice:allsPrice
            })
        }
      } else {
        if (that.data.checked2) {
            let allsPrice = that.data.shopPrice2 * that.data.quantity2;
            that.setData({
                totalPrice:allsPrice
            })
        } else {
            that.setData({
                totalPrice:0
            })
        }
      }
    }
  },
  allElection: function () {
    let that = this;
    if (alls) {
      let allsPrice = (that.data.shopPrice1 * that.data.quantity1) + (that.data.shopPrice2 * that.data.quantity2);
      that.setData({
        checked0: true,
        checked1: true,
        checked2: true,
        checked3: true,
        settlement: false,
        totalPrice:allsPrice
      }, () => { 
          alls = false;
          shopGoods = false;
      })
    } else {
      that.setData({
        checked0: false,
        checked1: false,
        checked2: false,
        checked3: false,
        settlement: true,
        totalPrice:0
      }, () => { 
          alls = true;
          shopGoods = true;
      })
    }
  },
  buyerCarDel: function () {
     wx.showModal({
      title: '',
      content: '确认要删除?',
      cancelColor: '#999999',
      confirmColor:'#ff0028',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  buyercarSettlement: function () {
    wx.navigateTo({
       url: '/pages/confirmorder/confirmorder'
    })
  },
  // bigScan: function () {
  //    wx.scanCode({
  //     scanType:['barCode', 'qrCode','datamatrix','pdf417'],
  //     success (res) {
  //         console.log(res)
  //      }
  //   })
  // }
})
